package com.etc.my.controller;

import com.etc.my.dto.*;
import com.etc.my.entity.User;
import com.etc.my.service.ReserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class ReserveController {
    @Autowired
    private ReserveService rs;

    public ReserveService getRs() {
        return rs;
    }

    public void setRs(ReserveService rs) {
        this.rs = rs;
    }

    @RequestMapping("/getReserveByUserId")
    @ResponseBody
    public MyMessage getReserveByUserId(HttpSession session, Integer page, Integer size) {
        MyMessage message = new MyMessage();
        //获取登录的用户
User user = (User) session.getAttribute("loginuser");

        if (user != null) {
            //判断是否有取到值
            if (page == null || page <= 0) {
                page = 1;
            }

            if (size == null || size <= 0) {
                size = 3;
            }
            //获取总条数
            Integer allCount = rs.getReserveCountByUserId(user.getUser_id()).getAllCount();
            if (allCount >= 0 && allCount != null) {
                Integer maxPage = (allCount % size == 0) ? (allCount / size) : (allCount / size + 1);
                //页数条数上限容错
                page = page > maxPage ? maxPage : page;
                size = size > 3 ? 3 : size;
                //页数条数下限容错
                page = page <= 0 ? 1 : page;
                size = size <= 0 ? 3 : size;

                PageDto pd = new PageDto();
                pd.setPage(page);
                pd.setSize(size);

                //调用方法查询
                List<ReserveAndCarMessageDto> pushList = rs.getReserveByUserId(user.getUser_id(), (page - 1) * size, size);
                if (pushList.size() > 0 && pushList != null) {
                    message.setFlag(1);
                    pd.setMaxPage(maxPage);
                    pd.setAllCount(allCount);
                    pd.setOlist(pushList);
                    message.setData(pd);
                } else {
                    message.setFlag(0);
                    message.setInfo("查无此纪录");
                }
            } else {
                message.setFlag(0);
                message.setInfo("查无此纪录");
            }

        } else {
            message.setFlag(0);
            message.setInfo("您尚未登录请先登录!");
        }

        return message;
    }

    /*
    添加预约信息
     */
    @RequestMapping(value = "/addReserveMsg")
    public void addReserveMsg(ReserveDto reserveDto, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw = response.getWriter();
        //判断信息框是否有填写（是否为空）
        if (reserveDto.getReserve_phone()!=null&&reserveDto.getReserve_car()!=null&&reserveDto.getReserve_date()!=null){
            //将信息框内信息存入dto
            if (rs.addReserveMsg(reserveDto)){
                pw.print("<script>alert('预约成功');location.href='index.jsp'</script>");
            }else {
                pw.print("<script>alert('预约失败');history.go(-1)</script>");
            }
        }

        pw.flush();
        pw.close();
    }

    @RequestMapping("/cancelReserveMsg")
    public void cancelReserveMsg(HttpServletResponse response, Integer reserve_id)throws Exception{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if(reserve_id==null || reserve_id<0){
            out.print("<script>alert('取消失败，参数错误！');history.go(-1)</script>");
            System.out.println(reserve_id);
        }else{
            if(rs.deleteReserveMsg(reserve_id)){
                out.print("<script>alert('取消成功！');location.href='myreserve.jsp'</script>");
            } else{
                out.print("<script>alert('取消失败,请重新再试...');history.go(-1)'</script>");
            }
        }
        out.flush();
        out.close();
    }

}
